package ink.yql.controller;

import ink.yql.model.Dynamic;
import ink.yql.service.DynamicService;
import ink.yql.util.RestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author YQL
 */
@RestController
@RequestMapping("/dynamic")
public class DynamicController {
    @Autowired
    private DynamicService dynamicService;

    @GetMapping("/{sendTime}")
    public RestObject getByUid(@PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请0求时间超时").build();
        }
        return dynamicService.getByUid();
    }

    @GetMapping("/{uid}/{sendTime}")
    public RestObject getByUid(@PathVariable("uid") Integer uid,@PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请0求时间超时").build();
        }
        return dynamicService.getByUid(uid);
    }
    @PostMapping("/{sendTime}")
    public RestObject addOne(@RequestBody Dynamic dynamic, @PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请0求时间超时").build();
        }
        return dynamicService.addOneDynamic(dynamic);
    }

    @DeleteMapping("/{did}/{sendTime}")
    public RestObject addOne(@PathVariable("did") Integer did, @PathVariable("sendTime") Long sendTime) {
        // 保护措施  以防拿到url一直请求
        if (System.currentTimeMillis() / 1000 - sendTime > 3) {
            return RestObject.builder().code("400").message("请0求时间超时").build();
        }
        return dynamicService.deleteByDid(did);
    }

}
